/*	$Id: if_bridge.c 6 2008-07-29 02:30:10Z phrakt $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHBRIDGE IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

#if defined(__OpenBSD__) || defined(__NetBSD__)
#include <net/if_bridge.h>
#endif

#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "if.h"
#include "dlink.h"
#include "pforge.h"


#define PFIF_BRIDGE_ADDRLEN       6     /* address length in bytes */
#define PFIF_BRIDGE_TYPELEN       2     /* EtherType length */
#define PFIF_BRIDGE_CRCLEN        4     /* Cyclic Redundancy Check length */
#define PFIF_BRIDGE_MINFRMLEN    64     /* minimum frame length */
#define PFIF_BRIDGE_MAXFRMLEN  1518     /* max frame length */

#define PFIF_BRIDGE_HDRLEN ((PFIF_BRIDGE_ADDRLEN * 2) + PFIF_BRIDGE_TYPELEN)

#define PFIF_BRIDGE_MAXMTU \
	(PFIF_BRIDGE_MAXFRMLEN - PFIF_BRIDGE_HDRLEN - PFIF_BRIDGE_CRCLEN)
#define PFIF_BRIDGE_MINMTU \
	(PFIF_BRIDGE_MINFRMLEN - PFIF_BRIDGE_HDRLEN - PFIF_BRIDGE_CRCLEN)

#define PFIF_BRIDGE_ADDRFMT   "%02x:%02x:%02x:%02x:%02x:%02x"


int   pf_bridge_init    (void);
void  pf_bridge_fini    (void);
int   pf_bridge_create  (PF_NETIF *);
int   pf_bridge_delete  (PF_NETIF *);
void  pf_bridge_input   (PF_NETIF *);
void  pf_bridge_output  (PF_NETIF *);
int   pf_bridge_ctl     (PF_NETIF *);
void  pf_bridge_print   (PF_PKT *);





/* bridge-specific data */
struct pf_bridge_data {

};



struct pf_netif_ops pf_bridge_ops = {
	pf_bridge_init,
	pf_bridge_fini,
	NULL,
	NULL,
	pf_bridge_input,
	pf_bridge_output,
	NULL,
	pf_bridge_print
};





/*
 * pf_bridge_init()
 *
 * Initialization routine for the Ethernet
 */

int
pf_bridge_init(void)
{
	return (0);
}


/*
 * pf_bridge_fini()
 *
 */

void
pf_bridge_fini(void)
{

}


int
pf_bridge_create(PF_NETIF *ifp)
{



}


int
pf_bridge_delete(PF_NETIF *ifp)
{


}


/*
 * pf_bridge_input()
 *
 * Input routine to read Ethernet frames and decapsulate the Ethernet layer.
 */

void
pf_bridge_input(PF_NETIF *ifp)
{
	int ret;
	uint8_t bigbuf[PFIF_BRIDGE_MAXFRMLEN];
	PFPKT *pkt;
	struct pf_bridgehdr *hdr;

	pkt = pf_pkt_alloc();
	if (pkt == NULL) {
		return;
	}

#if 0
	ret = dlink_read(ifp->ifdl, bigbuf, sizeof(bigbuf));
	if (ret < 0) {
	}
#endif

	hdr = (struct pf_bridgehdr *)bigbuf;
	pf_bridge_print(hdr);
}


/*
 * pf_bridge_output()
 *
 */

void
pf_bridge_output(PF_NETIF *ifp)
{
	uint8_t bigbuf[2048];
	struct pf_bridgehdr *hdr;

#if 0
	if (flen < PFIF_BRIDGE_MINFRMLEN) {
		/* pad with 0's */
	}
#endif

#if 0
	if (dlink_write(ifp->ifdl, bigbuf, sizeof(bigbuf)) < 0) {
	}
#endif

	hdr = (struct pf_bridgehdr *)bigbuf;
}


/*
 * pf_bridge_print()
 *
 * Print a human-readable version of the data contained in an Ethernet header.
 */

void
pf_bridge_print(PF_PKT *pkt)
{
	struct pf_bridge_hdr *hdr;


	printf("Dst: " PFIF_BRIDGE_ADDRFMT,
	    hdr->eh_dst[0], hdr->eh_dst[1], hdr->eh_dst[2],
	    hdr->eh_dst[3], hdr->eh_dst[4], hdr->eh_dst[5]);

	printf("   Src: " PFIF_BRIDGE_ADDRFMT,
	    hdr->eh_src[0], hdr->eh_src[1], hdr->eh_src[2],
	    hdr->eh_src[3], hdr->eh_src[4], hdr->eh_src[5]);

	printf("   Type: 0x%x\n", ntohs(hdr->eh_type));
}
